package com.samehope.ar.controller;


import com.samehope.ar.common.CommonResult;
import com.samehope.ar.dto.ProjectAddParam;
import com.samehope.ar.dto.ProjectTeamUpdateParam;
import com.samehope.ar.dto.ProjectUpdateParam;
import com.samehope.ar.service.IProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 项目 前端控制器
 * </p>
 *
 * @author Zhang Luo
 * @since 2019-12-23
 */
@RestController
@RequestMapping("/project")
public class ProjectController {

    @Autowired
    private IProjectService projectService;

    /**
     * 新增项目接口
     * @return
     */
    @PreAuthorize("hasRole('ROLE_COMPANY')")
    @PostMapping("/add")
    public CommonResult add(@RequestBody @Valid ProjectAddParam param) {
        projectService.add(param);
        return CommonResult.success(null);
    }

    @PreAuthorize("hasRole('ROLE_COMPANY') or hasRole('ROLE_PROJECT')")
    @PutMapping("/update")
    public CommonResult update(@RequestBody @Valid ProjectUpdateParam param){
        projectService.update(param);
        return CommonResult.success(null);
    }

    @PreAuthorize("hasRole('ROLE_COMPANY') or hasRole('ROLE_PROJECT')")
    @DeleteMapping("/del/{projectId}")
    public CommonResult del(@PathVariable("projectId") Long projectId){
        projectService.del(projectId);
        return CommonResult.success(null);
    }

    @GetMapping("/findByCompany")
    public CommonResult findByCompany(@RequestParam(required = true) Long userId){
        return CommonResult.success(projectService.findByCompany(userId));
    }

    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @GetMapping("/detail")
    public CommonResult detail(@RequestParam(required = true) Long projectId){
        return CommonResult.success(projectService.detail(projectId));
    }
}
